<template>
    <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
          <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <div  class="search">
          <el-button type="primary" @click="save">添加角色</el-button>
        </div>
        <el-table
    :data="tableData"
    style="width: 100%;margin-bottom: 20px;"
    
    border
    
    >
    <el-table-column type="expand">
      <template slot-scope="scope">
          <p v-if="scope.row.children.length != 0">
              <span v-for="(item,i) in scope.row.children" :key="i">
                <el-tag>{{item.authName}}</el-tag>
                <span v-for="(ele,index) in item.children" :key="index">
                  <el-tag type="success">{{ele.authName}}</el-tag>
                  <span v-for="(rig,j) in ele.children" :key="j">
                  <el-tag type="warning">{{rig.authName}}</el-tag>
                </span>
              </span>
              </span>
            </p>
      </template>
    </el-table-column>
    <el-table-column label="#" type="index"> </el-table-column>
    <el-table-column
      prop="roleName"
      label="角色名称"
      >
    </el-table-column>
    <el-table-column
      prop="roleDesc"
      label="角色描述"
      sortable
     >
    </el-table-column>
    <el-table-column
      prop="address"
      label="操作">
       <template slot-scope="scope">
            <el-button size="mini" type="primary" class="el-icon-edit"
              @click="edit(scope.row)">编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="del(scope.row.id)"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              class="el-icon-setting"
              @click="adress(scope.row.id)"
              >分配权限</el-button
            >
          </template>
    </el-table-column>
  </el-table>
  <!-- 添加模态框 -->
  <el-dialog
  :title="id==0?'添加':'编辑'"
  :visible.sync="dialogVisible"
  width="30%"
  >
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="角色名称">
      <el-input v-model="form.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色描述">
      <el-input v-model="form.roleDesc"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="add">确 定</el-button>
  </span>
</el-dialog>
<!-- 权限管理模态框 -->

<el-dialog
  title="提示"
  :visible.sync="dialog"
  width="30%"
 >
  <el-tree
  :data="tree"
  show-checkbox
  default-expand-all
  node-key="id"
  ref="tree"
  highlight-current
  :props="defaultProps">
</el-tree>

  <span slot="footer" class="dialog-footer">
    <el-button @click="dialog = false">取 消</el-button>
    <el-button type="primary" @click="ok">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>
<script>
import {getRoles,addRoles,editRoles,delRoles,getCate,getRolesList} from"../../api/user.js"
export default {
    data() {
        return {
            tableData:[],
            dialogVisible:false,
            form:{
               roleName:"",
               roleDesc:""
            },
            id:0,
            cateId:0,
            dialog:false,
            defaultProps: {
              children: "children",
              label: "authName",
            },
            tree:[]
        }
    },
    methods: {
        getList(){
            getRoles().then(res=>{
                console.log(res);
                this.tableData=res.data.data
            })
        },
        //添加打开模态框
        save(){
          this.dialogVisible=true
        },
        //添加角色
        add(){
          if(this.id!=0){
            editRoles({id:this.id,roleName:this.form.roleName,roleDesc:this.form.roleDesc}).then(res=>{
              console.log(res);
              this.getList()
              this.form={}
              this.id=0
            })
          }else{
            addRoles(this.form).then(res=>{
               console.log(res);
               this.getList()
            })
          }
          
          this.dialogVisible=false
        },
        //修改角色
        edit(item){
          this.dialogVisible=true
          this.form.roleName=item.roleName
          this.form.roleDesc=item.roleDesc
          this.id=item.id
        },
        //删除
        del(id){
           this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delRoles(id).then(res=>{
            console.log(res);
            this.$message({
            type: 'success',
            message: '删除成功!'
          });
            this.getList()
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        },
        //权限管理
        adress(id){
           this.dialog=true
           this.cateId=id
        },
        ok(){
           let arr1 = this.$refs.tree.getCheckedKeys();
           let arr2 = this.$refs.tree.getHalfCheckedKeys();
           getCate({id:this.cateId,rids: [...arr1, ...arr2].join(",")}).then(res=>{
              console.log(res);
              this.dialog=false
              this.getList()
           })
        }
    },
    created() {
        this.getList()
        getRolesList().then(res=>{
          console.log(res);
          this.tree=res.data.data
        })
    },
}
</script>
<style lang="scss" scoped>
.search {
  width: 700px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
